package TreadPool;

import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPoolExecutor2{
    private ArrayList<Thread>threadArrayList=new ArrayList<>();
    private BlockingQueue<Runnable>queue=new ArrayBlockingQueue<>(1000);
    public MyThreadPoolExecutor2(int n){
        for(int i=0;i<n;i++){
            Thread t=new Thread(()->{
                while(true){
                    Runnable runnable= null;
                    try {
                        runnable = queue.take();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    runnable.run();
                }
            });
            t.start();
            threadArrayList.add(t);
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

}

public class Main2 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor2 myThreadPoolExecutor2=new MyThreadPoolExecutor2(4);
        for(int i=0;i<1000;i++){
            int n=i;
            myThreadPoolExecutor2.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行任务"+n+"当前线程是"+Thread.currentThread().getId());
                }
            });
        }
    }
}
